package org.groupg.practice.lang;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.StopWatch;

import java.time.Duration;
import java.time.format.DateTimeFormatter;

public class StopWatchExample {
    public static void main(String[] args) throws InterruptedException {
        StopWatch stopWatch = new StopWatch();

        // 启动计时器
        stopWatch.start();

        // 模拟一些处理（例如，线程休眠1秒）
        Thread.sleep(1234);

        // 停止计时器
        stopWatch.stop();

        // 获取经过的时间（毫秒）
        long elapsedMillis = stopWatch.getTime();

        // 使用 Java 的 Duration 和 DateTimeFormatter 将时间格式化为人类可读的形式
        Duration duration = Duration.ofMillis(elapsedMillis);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("mm:ss.SSS");
        String humanReadableTime = duration.toMinutesPart() + ":"
                + String.format("%02d", duration.getSeconds()) + "."
                + String.format("%03d", duration.getNano() / 1_000_000);

        // 打印人类可读的时间
        System.out.println("Elapsed Time: " + humanReadableTime);

        StringUtils.isNotBlank(humanReadableTime);
    }
}